<?

namespace App\Servers\WebSocket\Logics;

use App\Servers\WebSocket\abstracts\LogicAbstract;
use App\Servers\WebSocket\Utils\JWT;

class AccountManage extends LogicAbstract
{

    // 登录
    public function login($data): array
    {
        $pdo = $this->threadTool['pdo']->get();
        // 准备预处理语句
        $stmt = $pdo->prepare("SELECT id,password_hash,username,nickname,email,phone,gender,birthday,avatar,register_time,register_ip,last_login_time,last_login_ip FROM users WHERE username=:username");

        // 绑定参数并执行
        $stmt->execute([':username' => $data->username]);
        $user = $stmt->fetch();
        if ($user && isset($data->password) && password_verify($data->password, $user['password_hash'])) {
            $token = JWT::encode(['user_id' => $user['id']]);   //jwt令牌
            $exp = time() + 7 * 24 * 60 * 60;
            unset($user['password_hash']);
            $refreshToken = JWT::encode(['user_id' => $user['id'], 'exp' => $exp]); // 刷新令牌

            // 准备预处理语句
            $stmt = $pdo->prepare("UPDATE users SET refresh_token=:refresh_token, last_login_time=:last_login_time WHERE id=:user_id");
            // 绑定参数并执行
            $stmt->execute([':user_id' => $user['id'], ':refresh_token' => $refreshToken, 'last_login_time' => date('Y-m-d H:i:s')]);
            $rowCount = $stmt->rowCount();

            $this->threadTool['pdo']->put($pdo);
            return ['code' => 1, 'msg' => '登录成功', 'token' => $token, 'refreshToken' => $refreshToken, "expiresIn" => $exp, 'userInfo' => $user];
        } else {
            $this->threadTool['pdo']->put($pdo);
            return ['code' => 1000, 'msg' => '登录失败'];
        }
    }
    //注册
    public function sign($data)
    {
        $pdo = $this->threadTool['pdo']->get();
        // 准备预处理语句
        $stmt = $pdo->prepare("INSERT INTO users (username, password_hash, salt, email, phone, birthday, register_time)  VALUES (:username, :password_hash, :salt, :email, :phone, :birthday, :register_time)");

        // 绑定参数并执行
        $stmt->execute([
            ':username' => $data->username,
            ':password_hash' => password_hash($data->password, PASSWORD_DEFAULT),
            ':salt' => '',
            ':email' => '',
            ':phone' => '',
            ':birthday' => date('Y-m-d'),
            ':register_time' => date('Y-m-d H:i:s'),
        ]);
        $rowCount = $stmt->rowCount();
        $this->threadTool['pdo']->put($pdo);

        if ($rowCount) { //创建成功
            return ['code' => 1, 'msg' => '注册成功'];
        } else {
            return ['code' => 1000, 'msg' => '注册失败'];
        }
    }

    public function logout($data): array
    {
        if (isset($data->refreshToken) && JWT::isExpire($data->refreshToken)) {
            $payload = JWT::decode($data->refreshToken);
            $pdo = $this->threadTool['pdo']->get();
            // 准备预处理语句
            $stmt = $pdo->prepare("UPDATE users SET refresh_token='', last_login_time=:last_login_time WHERE id=:user_id");

            // 绑定参数并执行
            $stmt->execute([':user_id' => $payload["user_id"], 'last_login_time' => date('Y-m-d H:i:s')]);
            $rowCount = $stmt->rowCount();
            $this->threadTool['pdo']->put($pdo);
            return ['code' => 1, 'msg' => '退出登录成功'];
        } else {
            return ['code' => 1000, 'msg' => '退出登录失败'];
        }
    }
}
